package com.qsd.doc4jsimpple.util;

import cn.hutool.core.collection.CollectionUtil;
import com.qsd.doc4jsimpple.constants.ListTypeEnum;
import com.qsd.doc4jsimpple.item.MarkStyle;
import net.minidev.json.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * * @Author xujunxia
 * * @Date 2024-11-05 15:48
 * * @Description TODO
 * * wb-0455@dtdream.com
 **/
public class CollectionTool {

    public static  boolean containsValueRecursive(Map<String, Object> hashMap, String value) {
        if(CollectionUtil.isEmpty(hashMap)){
            return false;
        }
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            Object val = entry.getValue();
            if (value.equals(val)) {
                return true;
            }
            if (val instanceof Map) {
                // 如果值是一个Map类型，继续递归查找
                Map<String, Object> subMap = (Map<String, Object>) val;
                if (containsValueRecursive(subMap, value)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static Map<Integer,List<Integer>> setDocxNumIdToMap(JSONArray jsonArray ,Map<Integer,List<Integer>> map,Map<Integer,List<Integer>> bulletMap,int type,Integer listNum){
        Integer start = getMaxNumberIdInMap(map,bulletMap)+1;
        List<Integer> numberList = new ArrayList<Integer>();
        for(int i=0;i<jsonArray.size();i++){
            numberList.add(start+i);
        }
        if(ListTypeEnum.ORDER_LIST.getCode()==type){
            map.put(listNum,numberList);
            return map;
        }else if(ListTypeEnum.BULLET_LIST.getCode()==type){
            bulletMap.put(listNum,numberList);
            return bulletMap;
        }

        return null;

    }

    public static Integer getMaxNumberIdInMap(Map<Integer,List<Integer>> map,Map<Integer,List<Integer>> bulletMap ){
        if(CollectionUtil.isEmpty(map) && CollectionUtil.isEmpty(bulletMap)){
            return 0;
        }
        Integer maxNumberId = 0;
        for(Integer key:map.keySet()){
            List<Integer> numberList = map.get(key);
            if(CollectionUtil.isNotEmpty(numberList)){
                for(Integer number:numberList){
                    if(maxNumberId<number){
                        maxNumberId = number;
                    }
                }
            }

        }
        for(Integer key:bulletMap.keySet()){
            List<Integer> numberList = bulletMap.get(key);
            if(CollectionUtil.isNotEmpty(numberList)){
                for(Integer number:numberList){
                    if(maxNumberId<number){
                        maxNumberId = number;
                    }
                }
            }

        }
        return maxNumberId;
    }

    public static  Object getStyleInVal(Map<String, Object> hashMap, String value) {
        if(CollectionUtil.isEmpty(hashMap)){
            return false;
        }
        for (Map.Entry<String, Object> entry : hashMap.entrySet()) {
            Object val = entry.getValue();
            if (value.equals(val)) {
                return val;
            }
            if (val instanceof Map) {
                // 如果值是一个Map类型，继续递归查找
                Map<String, Object> subMap = (Map<String, Object>) val;
                if (containsValueRecursive(subMap, value)) {
                    return getStyleInVal(subMap,value);
                }
            }
        }
        return false;
    }

    public static  boolean containsMarkStyleRecursive(List<MarkStyle> marks, String value) {
        if(CollectionUtil.isEmpty(marks)){
            return false;
        }
        for(MarkStyle markStyle : marks){
            if(value.equals(markStyle.getType())){
                return true;
            }

            if(containsValueRecursive(markStyle.getAttrs(),value)){
                return true;
            }
            if(hasKeyRecursively(markStyle.getAttrs(),value)){
                return true;
            }

        }
        return false;
    }

    public static  Object getStyleInMarkStyle(List<MarkStyle> marks, String style) {
        if(CollectionUtil.isEmpty(marks)){
            return null;
        }
        for(MarkStyle markStyle : marks){
            if(style.equals(markStyle.getType())){
                return true;
            }

            if(containsValueRecursive(markStyle.getAttrs(),style)){
                return getStyle(markStyle.getAttrs(),style);
            }
            if(hasKeyRecursively(markStyle.getAttrs(),style)){
                return getStyle(markStyle.getAttrs(),style);
            }

        }
        return null;
    }

    public static  Object getStyleInMarkStyle(List<MarkStyle> marks,String markType, String style) {
        if(CollectionUtil.isEmpty(marks)){
            return null;
        }
        for(MarkStyle markStyle : marks){
            if(markType.equals(markStyle.getType())){
                if(containsValueRecursive(markStyle.getAttrs(),style)){
                    return getStyle(markStyle.getAttrs(),style);
                }
                if(hasKeyRecursively(markStyle.getAttrs(),style)){
                    return getStyle(markStyle.getAttrs(),style);
                }
            }
        }
        return null;
    }



    public static boolean containStyle(Map<String, Object> attrs, List<MarkStyle> marks, String style){
        if(hasKeyRecursively(attrs,style)||containsValueRecursive(attrs,style)){
            return true;
        }
        if(containsMarkStyleRecursive(marks,style)){
            return true;
        }
        return false;
    }

    public static Object getStyle(Map<String, Object> attrs,List<MarkStyle> marks, String style){
        if(hasKeyRecursively(attrs,style)){
            return getStyle(attrs,style);
        }
        if(containsValueRecursive(attrs,style)){
            return getStyleInVal(attrs,style);
        }

        if(containsMarkStyleRecursive(marks,style)){
            return getStyleInMarkStyle(marks,style);
        }
        return null;
    }


    public static boolean hasKeyRecursively(Map<String, Object> map, String targetKey) {
        if(CollectionUtil.isEmpty(map)){
            return false;
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.equals(targetKey)) {
                return true;
            }
            Object value = entry.getValue();
            if (value instanceof Map) {
                // 如果值是一个Map类型，继续递归查找
                Map<String, Object> subMap = (Map<String, Object>) value;
                if (hasKeyRecursively(subMap, targetKey)) {
                    return true;
                }
            }
        }
        return false;
    }

    public static Object getStyle(Map<String, Object> map, String targetKey) {
        if(CollectionUtil.isEmpty(map)){
            return null;
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.equals(targetKey)) {
                return entry.getValue();
            }
            Object value = entry.getValue();
            if (value instanceof Map) {
                // 如果值是一个Map类型，继续递归查找
                Map<String, Object> subMap = (Map<String, Object>) value;
                if (hasKeyRecursively(subMap, targetKey)) {
                    return getStyle(subMap,targetKey);
                }
            }
        }
        return false;
    }


    public static boolean getValuesInAttrs(Map<String, Object> map, String targetKey) {
        if(CollectionUtil.isEmpty(map)){
            return false;
        }
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            if (key.equals(targetKey)) {
                return true;
            }
            Object value = entry.getValue();
            if (value instanceof Map) {
                // 如果值是一个Map类型，继续递归查找
                Map<String, Object> subMap = (Map<String, Object>) value;
                if (hasKeyRecursively(subMap, targetKey)) {
                    return true;
                }
            }
        }
        return false;
    }
}
